// pages/user/myRest/myRest.js
var util = require('../../utils/util');
let App = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        dte: [], //日期
        years: '', //年
        months: '', //月  
        coach_id: '', //当前id
        da: '', //日
        weeks: [], //休息星期
        the_time: '', //休息时间
        list: [], //我的休息信息数组
        date: '',//传入时间
        date_month: '',//显示时间
        information_list:[],

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var This = this
        var time = util.formatTime(new Date());
        var date_value = time.slice(0, 4) + "-" + time.slice(5, 7)
        var date_value_month = time.slice(0, 4) + "-" + time.slice(5, 7)
        console.log(date_value)
        This.setData({
            date: date_value,
            date_month: date_value_month,
        })
        This.getarray();
    },

    getarray: function() {
        var This = this;
        App._get('coach/personal/Rest', {
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            date_str: This.data.date_month
        }, function(result) {
            if (result.code == 200) {
                // for (var i = 0; i < result.list.length; i++) {
                //     var data = result.list[i].date_str
                //     This.years = data.split("-")[0] + '年' + '-';
                //     This.months = data.split("-")[1] + '月' + '-';
                //     This.da = data.split("-")[2] + '日';
                //     This.data.coach_id = result.list[i].id;
                //     var s = This.years + This.months + This.da;
                //     This.data.dte.push(s)
                //     This.data.weeks.push('星期' + result.list[i].week);
                // }
                This.setData({
                    // date_str: This.data.dte,
                    // week: This.data.weeks,
                    list: result.list,
                })
            } else {
                wx.showToast({
                    title: result.error,
                    icon: 'none',
                    duration: 2000,
                })
            }
        })
    },
    bindDateChange: function (e) {
        var This = this;
        This.setData({
            information_list: []
        })
        var date_month = e.detail.value
        var year = date_month.slice(0, 4)
        var month = date_month.slice(5, 7)
        var date_time = year + '-' + month
        This.setData({
            date_month: date_time,
            date: e.detail.value
        })
        This.getarray();
    },
    add_to() {
        wx.navigateTo({
            url: '/pages/addRest/addRest',
        })
    },
    del(e) {
        var This = this;
        // This.data.coach_id = e.target.dataset.id
        This.setData({
            coach_id: e.target.dataset.id
        })
        App._get('coach/personal/rest_cancel', {
            id: This.data.coach_id,
            phone: wx.getStorageSync('phone'),
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id')
        }, function(result) {
            if (result.status == 1) { 
                wx.showToast({
                    title: '取消成功',
                    icon: 'none',
                    duration: 2000,
                })
                This.getarray();
            } else {
                wx.showToast({
                    title: result.error,
                    icon: 'none',
                    duration: 2000,
                })
            }
        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        // wx.reLaunch({
        //   url: "../../user"
        // })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})